﻿#include "FITKAbaqusGetInterfaceTool.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Component/FITKAbaqusData/FITKAbaqusPart.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaAssembly.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSurfaceLabel.h"
#include "FITK_Interface/FITKInterfaceModel/FITKModelSet.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurfaceNode.h"
#include "FITK_Interface/FITKInterfaceModel/FITKMeshSurfaceElement.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

namespace IO
{
    void FITKAbaqusGetInterfaceTool::setCaseData(AbaqusData::FITKDataCase * caseData, int partID)
    {
        _caseData = caseData;
        _partID = partID;
    }

    int FITKAbaqusGetInterfaceTool::getSetIDByName(QString setName, int type, int & id)
    {
        if (!_caseData)
        {
            consoleMessage(3, QString("CaseData No Setting"));
            return -1;
        }
        Interface::FITKModelSet* setData = getSetIDByNamePrivate(setName, id);
        if (!this->isContainSetType(setData, type))
        {
            consoleMessage(3, QString("Get Set Name Error, Name=%1").arg(setName));
            return -1;
        }
        return setData->getDataObjectID();
    }

    int FITKAbaqusGetInterfaceTool::createSetByStrData(QString strData, int type, int& id)
    {
        if (!_caseData)
        {
            consoleMessage(3, QString("CaseData No Setting"));
            return -1;
        }
        Interface::FITKAbaAssembly* assembly = _caseData->getAssembly();
        if (!assembly) return -1;
        bool isNum = false;
        QString modelString;
        int idData = -1;
        QStringList setStrList = strData.split('.');
        if (setStrList.size() == 1)
            idData = setStrList.at(0).toInt(&isNum);
        else if (setStrList.size() == 2)
        {
            modelString = setStrList.at(0);
            idData = setStrList.at(1).toInt(&isNum);
        }
        if (!isNum) return -1;
        Interface::FITKAbstractModel* modelData = nullptr;
        if (modelString.isEmpty())
            modelData = assembly;
        else
            modelData = assembly->getDataByName(modelString);
        if (modelData == nullptr) return -1;
        id = modelData->getDataObjectID();
        Interface::FITKComponentManager* setSurfMgr = assembly->getComponentManager();
        if (!setSurfMgr) return -1;
        Interface::FITKModelSet* setData = new Interface::FITKModelSet((Interface::FITKModelEnum::FITKModelSetType)type);
        setSurfMgr->appendDataObj(setData);
        setData->setModel(modelData);
        setData->setAbsoluteMember(QList<int>() << idData);
        return setData->getDataObjectID();
    }

    int FITKAbaqusGetInterfaceTool::getSurfIDByName(QString surfName, int type, int & id)
    {
        if (!_caseData)
        {
            consoleMessage(3, QString("CaseData No Setting"));
            return -1;
        }
        int surfID = getSurfIDByNamePrivate(surfName, type, id);
        if (surfID == -1)
            consoleMessage(3, QString("Get Surf Name Error, Name=%1").arg(surfName));
        return surfID;
    }

    int FITKAbaqusGetInterfaceTool::createSurfByStrData(QString strData, QString polygonData, int type, int& id)
    {
        if (!_caseData)
        {
            consoleMessage(3, QString("CaseData No Setting"));
            return -1;
        }
        Interface::FITKAbaAssembly* assembly = _caseData->getAssembly();
        if (!assembly) return -1;
        id = assembly->getDataObjectID();
        int setID = -1;
        int instanceId = -1;
        if (type == Interface::FITKMeshSurface::MeshSurfaceType::SurNode)
        {
            setID = this->createSetByStrData(strData, Interface::FITKModelEnum::FITKModelSetType::FMSNode, instanceId);
        }
        else if (type == Interface::FITKMeshSurface::MeshSurfaceType::SurEle)
        {
            setID = this->createSetByStrData(strData, Interface::FITKModelEnum::FITKModelSetType::FMSElem, instanceId);
        }
        Interface::FITKAbaSurfaceLabel::AbaSurfLabel polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::Empty;
        if (polygonData.toLower() == "p1")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::S1;
        else if (polygonData.toLower() == "p2")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::S2;
        else if (polygonData.toLower() == "p3")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::S3;
        else if (polygonData.toLower() == "p4")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::S4;
        else if (polygonData.toLower() == "p5")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::S5;
        else if (polygonData.toLower() == "p6")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::S6;
        else if (polygonData.toLower() == "p")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::SNEG;
        else if (polygonData.toLower() == "-p")
            polygonType = Interface::FITKAbaSurfaceLabel::AbaSurfLabel::SPOS;

        if (type == Interface::FITKMeshSurface::MeshSurfaceType::SurNode)
        {

        }
        else if (type == Interface::FITKMeshSurface::MeshSurfaceType::SurEle)
        {
            Interface::FITKMeshSurfaceElement* surfData = new Interface::FITKMeshSurfaceElement;
            surfData->setModel(assembly);
            surfData->addMeshSet(instanceId, setID, polygonType);
            return surfData->getDataObjectID();
        }
        return -1;
    }

    Interface::FITKModelSet* FITKAbaqusGetInterfaceTool::getSetIDByNamePrivate(QString setName, int& modelID)
    {
        if (!_caseData)return nullptr;
        Interface::FITKAbaAssembly* assembly = _caseData->getAssembly();
        auto partManager = _caseData->getPartManager();
        if (!assembly || !partManager)return nullptr;
        AbaqusData::FITKAbaqusPart* part = partManager->getDataByID(_partID);
        //通过装配和part获取表面或集合管理器
        Interface::FITKComponentManager* setSurfManager = nullptr;
        if (part)
        {
            setSurfManager = part->getComponentManager();
            modelID = part->getDataObjectID();
        }
        else
        {
            setSurfManager = assembly->getComponentManager();
            modelID = assembly->getDataObjectID();
        }
        if (!setSurfManager)return nullptr;
        //获取集合
        QList<Interface::FITKModelSet*> setList = setSurfManager->getAllSet(false);
        for (auto set : setList)
        {
            if (set->getDataObjectName() == setName)
            {
                modelID = set->getAbsModelID();
                return set;
            }
                
        }
        ///*.*类型的名称集合数据获取
        QStringList str = setName.split('.');
        if (str.size() != 2)return nullptr;
        //获取instance的名称
        QString instanceName = str.at(0);
        //获取集合的名称
        setName = str.at(1);
        if (instanceName.isEmpty())return nullptr;
        //获取instance->instanceID
        Interface::FITKAbstractAssInstance* instance = assembly->getDataByName(instanceName);
        if (!instance)return nullptr;
        //重新获取模型id->instanceId
        modelID = instance->getDataObjectID();
        //获取part
        part = dynamic_cast<AbaqusData::FITKAbaqusPart*>(instance->getModel());
        if (!part)return nullptr;
        //获取part下的集合
        setSurfManager = part->getComponentManager();
        if (!setSurfManager)return nullptr;
        //通过类型获取链表
        setList = setSurfManager->getAllSet(false);
        for (auto set : setList)
        {
            if (set->getDataObjectName() == setName)
                return set;
        }
        return nullptr;
    }

    bool FITKAbaqusGetInterfaceTool::isContainSetType(Interface::FITKModelSet* setData, int type)
    {
        if (!setData)return false;
        if (type == -1)return true;
        //获取需要包含的集合类型
        Interface::FITKModelEnum::FITKModelSetType setType = (Interface::FITKModelEnum::FITKModelSetType)(type);
        //获取集合是否为复合集合
        if (!setData->isCombination())
        {
            //不是复合集合就直接判断本身类型是否是setType集合类型
            if (setData->getModelSetType() == setType)
                return true;
            return false;
        }
        //获取复合集合里的子集合数量
        int count = setData->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            //是复合集合就需要判断子集合是否包含setType集合类型
            Interface::FITKModelSet* childSetData = setData->getDataByIndex(i);
            if (childSetData && childSetData->getModelSetType() == setType)
                return true;
        }
        return false;
    }

    int FITKAbaqusGetInterfaceTool::getSurfIDByNamePrivate(QString name, int type, int &modelID)
    {
        if (!_caseData)return -1;
        Interface::FITKAbaAssembly* assembly = _caseData->getAssembly();
        auto partManager = _caseData->getPartManager();
        if (!assembly || !partManager)return -1;
        AbaqusData::FITKAbaqusPart* part = partManager->getDataByID(_partID);
        //通过装配和part获取表面或集合管理器
        Interface::FITKComponentManager* setSurfManager = nullptr;
        if (part)
        {
            setSurfManager = part->getComponentManager();
            modelID = part->getDataObjectID();
        }
        else
        {
            setSurfManager = assembly->getComponentManager();
            modelID = assembly->getDataObjectID();
        }
        if (!setSurfManager)return -1;
        //获取集合
        QList<Interface::FITKMeshSurface*> setSurfList;
        if (type == -1)
            setSurfList = setSurfManager->getAllSurface();
        else
            setSurfList = setSurfManager->getMeshSurface((Interface::MeshSurfaceType)type);
        for (auto surf : setSurfList)
        {
            if (surf->getDataObjectName() == name)
                return surf->getDataObjectID();
        }
        //获取*.*的类型数据
        QStringList str = name.split('.');
        if (str.size() != 2)return -1;
        QString instanceName = str.at(0);
        name = str.at(1);
        if (instanceName.isEmpty())
            return -1;
        //获取装配
        assembly = _caseData->getAssembly();
        // 获取instance->instanceID
        Interface::FITKAbstractAssInstance* instance = assembly->getDataByName(instanceName);
        if (!instance)return -1;
        modelID = instance->getDataObjectID();
        //获取part
        part = dynamic_cast<AbaqusData::FITKAbaqusPart*>(instance->getModel());
        if (!part)return -1;
        //获取part下的集合
        setSurfManager = part->getComponentManager();
        if (!setSurfManager)return -1;
        //通过类型获取链表
        if (type == -1)
            setSurfList = setSurfManager->getAllSurface();
        else
            setSurfList = setSurfManager->getMeshSurface((Interface::MeshSurfaceType)type);
        for (auto surf : setSurfList)
        {
            if (surf->getDataObjectName() == name)
                return surf->getDataObjectID();
        }
        return -1;
    }

    void FITKAbaqusGetInterfaceTool::consoleMessage(int level, const QString& str)
    {
        switch (level)
        {
        case 1:AppFrame::FITKMessageNormal(str); break;
        case 2: AppFrame::FITKMessageWarning(str); break;
        case 3:AppFrame::FITKMessageError(str); break;
        default: AppFrame::FITKMessageError(str); break;
        }
    }
}
